package com.hangzhou.xyj.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.OperationRespEvent;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.manager.NotifyManager;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.model.type.EPushMessageType;
import com.hangzhou.xyj.net.handler.MyLikeHandler;
import com.hangzhou.xyj.net.handler.TransferHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.MyLikeReq;
import com.hangzhou.xyj.net.request.TransferReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * Created by neo.duan on 2016/1/7.
 * 加载web页
 */
public class WebActivity extends BaseActivity implements View.OnClickListener {
    private WebView mWebView;
    private String url;
    private int mIntentTag;
    private String mTitle;
    private int mCommentId;//评论对象的Id

    private ViewGroup mLayoutRoot;
    private MyTextView mTvTransfer;//转发
    private MyTextView mTvComment;//评论
    private MyTextView mTvLike;//点赞
    private MyTextView mTvReport;//报名等
    private LinearLayout mLlContent;//下边的横条

    private String mShareTitle = "";//分享时候的标题，如：头条，演出，活动
    private String mShareContent = "";//分享的内容
    private String mShareUrl = "";//分享的url
    private int pushType;//推送的类型


    @Override
    public void initIntentData(Intent intent) {
        url = intent.getStringExtra(DataKey.INDEX);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, -1);
        mTitle = intent.getStringExtra(DataKey.TITLE);
        mCommentId = intent.getIntExtra(DataKey.OBJ_ID, -1);
        mShareContent = intent.getStringExtra(DataKey.SHARE_CONTENT);
        mShareUrl = intent.getStringExtra(DataKey.SHARE_URL);
        mShareTitle = intent.getStringExtra(DataKey.SHARE_TITLE);
        pushType = intent.getIntExtra(DataKey.PUSH_TYPE, -1);
    }

    @Override
    public void initTop() {
        if (StringUtil.isNotEmpty(mTitle)) {
            enableTitle(true, mTitle);
        } else {
            enableTitle(true, "标题");
        }

    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_web);
    }

    @Override
    public void initViews() {
        registerEventBus();
        mLayoutRoot = findView(R.id.layout_web_root_view);
        mWebView = findView(R.id.act_webview);
        mTvTransfer = findView(R.id.mtv_transfer);
        mTvComment = findView(R.id.mtv_comment);
        mTvLike = findView(R.id.mtv_like);
        mTvReport = findView(R.id.mtv_report);
        mLlContent = findView(R.id.ll_content);
        if (pushType != -1) {
            NotifyManager.getInstance().cancelMsg(mContext, EPushMessageType.valueOf(pushType));//清除通知
            //重置计数
            switch (EPushMessageType.valueOf(pushType)) {
                case SINGLE_CHAT:
                    NotifyManager.mSingleChatCount = 0;//重置
                    break;
                case GROUP_CHAT:
                    NotifyManager.mGroupChatCount = 0;
                    break;
                case ADD_GROUP:
                    NotifyManager.mAddGroupCount = 0;
                    break;
                default:
                case SYSTEM:
                    NotifyManager.mSystemCount = 0;
                    break;
            }
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void initDatas() {
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_HOT_SPOT://如果是热点
//                mShareTitle = "戏曲头条";
                mTvReport.setVisibility(View.GONE);
                break;
            case TAG_USER_AGREEMENT://如果是用户协议 进来的 隐藏
                mLlContent.setVisibility(View.GONE);
                break;
            case TAG_FIND_SHOW://如果是发现-演出
//                mShareTitle = "演出";
                mTvReport.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_big_book), null, null);
                mTvReport.setText("订票");
                break;
            case TAG_FIND_ACTION://如果是发现-活动
//                mShareTitle = "活动";
                mTvReport.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_big_sing_up), null, null);
                mTvReport.setText("报名");
                break;
            default:
                mLlContent.setVisibility(View.GONE);
                break;

        }
        mWebView.loadUrl(url);
    }

    @Override
    public void initListeners() {
        mTvComment.setOnClickListener(this);
        mTvTransfer.setOnClickListener(this);
        mTvLike.setOnClickListener(this);
        mTvReport.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {//未登录
        if (XYJApplication.getUserInfo() == null) {
            IntentManager.getInstance().goLoginActivity(mContext);
            return;
        }
        switch (v.getId()) {
            case R.id.mtv_transfer:
                PublicUtil.showShareContent(mShareTitle, mShareContent, mShareUrl, new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        afterTransFer(mCommentId, 1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                break;
            case R.id.mtv_comment:
                Intent intent = new Intent(mContext, CommentActivity.class);
                intent.putExtra(DataKey.OBJ_ID, mCommentId);
                intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_HOT_SPOT.getValue());
                IntentManager.getInstance().goActivity(mContext, intent);
                break;
            case R.id.mtv_like:
                like();
                break;
            case R.id.mtv_report:
                signup();//报名 订票
                break;
        }
    }

    /**
     * 跳转到报名，订票
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/21 10:13
     */
    private void signup() {
        Intent intent = new Intent(mContext, SignUpActivity.class);
        intent.putExtra(DataKey.INTENT_TAG, mIntentTag);
        intent.putExtra(DataKey.OBJ_ID, mCommentId);
        IntentManager.getInstance().goActivity(mContext, intent);
    }

    /**
     * 点赞
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/23 14:22
     */
    public void like() {
        MyLikeReq myLikeReq = new MyLikeReq(MyLikeReq.TYPE_ACTIVTTY, mCommentId, Integer.valueOf(CommonPreferences.getUserId(mContext)));
        HttpLoader.getInstance().sendRequest(new MyLikeHandler(myLikeReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("点赞成功!");
                    mWebView.reload();
                }
            }
        }, 0));
    }

    @Subscribe
    public void onEventMainThread(OperationRespEvent event) {
        if (event.isSuccess()) {
            //评论成功之后 刷新
            mWebView.reload();
        }
    }

    /**
     * 分享之后调用，用于统计分享的次数
     *
     * @param type  1：活动、演出，热点； 2：社群；
     * @param objId 被分享的对象的ID
     * @return
     * @author yaohuix
     * @date 2016/5/5 16:21
     */
    public void afterTransFer(int objId, int type) {
        TransferReq transferReq = new TransferReq(objId, type, mMyUserId);
        HttpLoader.getInstance().sendRequest(new TransferHandler(transferReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mWebView.reload();
            }
        }, 0));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegisterEventBus();
        if (null != mWebView) {
            //解除webView与父控件的依附关系
            mLayoutRoot.removeView(mWebView);
            mWebView.removeAllViews();
            mWebView.clearView();
            mWebView.setVisibility(View.GONE);
            mWebView.destroy();
        }
    }
}
